package com.graph;

public class Solution733 {
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        boolean[][] visited = new boolean[image.length][image[0].length];
        dfs(image, sr, sc, color, image[sr][sc], visited);
        return image;
    }

    public void dfs(int[][] image, int i, int j, int color, int curcolor, boolean[][] visited) {
        if (i < 0 || i >= image.length || j < 0 || j >= image[0].length) return;
        if (visited[i][j]) return;
        if (image[i][j] != curcolor) return;
        visited[i][j] = true;
        image[i][j] = color;
        dfs(image, i - 1, j, color, curcolor, visited);
        dfs(image, i + 1, j, color, curcolor, visited);
        dfs(image, i, j - 1, color, curcolor, visited);
        dfs(image, i, j + 1, color, curcolor, visited);
    }
}
